<?php


namespace App\Models;


use Illuminate\Database\Eloquent\Model;

class city extends Model
{
   protected $table="city";
   //查询所有的结果

    public function cityRows()
    {
        $cityRows=$this->get();
        return $this->cityRecursion($cityRows);
    }
    public function cityRecursion($rows,$pid=0,$level=0)
    {
        static $arr=array();
        foreach($rows as $v)
        {
            if($v['pid']==$pid)
            {
                $v['level']=$level;
                $arr[]=$v;
                $this->cityRecursion($rows,$v['id'],$level+1);
            }
        }
        return $arr;
    }


    public function children()
    {
        return $this->hasMany(get_class($this), 'pid', 'id');
    }

    public function allChildren()
    {
        return $this->children()->with('allChildren');
    }
}
